package foxesandrabbits.species;

import java.util.List;

import foxesandrabbits.field.Field;
import foxesandrabbits.field.Location;

/**
 * Vegetables are eaten by rabbits and which grow back some number of turns later.
 * 
 * @author Baptiste VIALE
 *
 */
public abstract class Vegetable extends AliveObject {
	
	private int TURNS_BEFORE_RESPAWN;
	private int remainingTurnsBeforeRespawn;
	
	public Vegetable(Field field, Location location, int turnsBeforeRespawn) {
		super(false, field, location,0);
		
		this.TURNS_BEFORE_RESPAWN = turnsBeforeRespawn;
		this.remainingTurnsBeforeRespawn = turnsBeforeRespawn;
	}
	
	/**
	 * Override the AliveObject giveBirth method
	 * Vegetables don't give birth, but they respawn after a while
	 * 
	 * @param newAnimals
	 */
	protected void giveBirth(List<AliveObject> newAnimals)
    {}
	
	/**
	 * Vegetables can respawn after a while
	 */
	public void respawn()
	{
		if (this.remainingTurnsBeforeRespawn <= 0)
		{
			if (this.getField().getObjectAt(this.getLocation()) == null)
				this.getField().place(this, this.getLocation());
			else
				return;
			
			this.alive = true;
			this.remainingTurnsBeforeRespawn = TURNS_BEFORE_RESPAWN;
			//System.out.println("A " + this.getClass().getName() + " has respawn");
		}
		else
			this.remainingTurnsBeforeRespawn --;
	}
	
	/**
	 * Override the AliveObject act method
	 * Call the respawn method if the Vegetable is not alive
	 * 
	 * @param newAnimals
	 */
    public void act(List<AliveObject> newAnimals)
    {
        if(!isAlive()) {
            respawn();           
        }
        else if (this.getField().getObjectAt(this.getLocation()) == null)
        {
        	this.getField().place(this, this.getLocation());
        }
    }
    
	protected void setDead()
	{
		alive = false;
	}
}
